package com.wuming.house.controller;

import com.wuming.house.core.CommonResult;
import com.wuming.house.core.JsonResult;
import com.wuming.house.model.House;
import com.wuming.house.model.HouseComment;
import com.wuming.house.model.WeixinUser;
import com.wuming.house.service.HouseCommentService;
import com.wuming.house.service.HouseService;
import com.wuming.house.service.WeixinUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author xuhuai
 * @createTime 2019/8/29 15:38
 * @Description:
 */
@Controller
@RequestMapping("/web/comment")
public class HouseCommentController extends BaseController{

    @Autowired
    private HouseCommentService houseCommentService;

    @Autowired
    private HouseService houseService;

    @Autowired
    private WeixinUserService weixinUserService;

    @RequestMapping("/toManageComment/{houseId}")
    public String toManageComment(@PathVariable Long houseId,Model model){
        model.addAttribute("houseId",houseId);
        return "/tools/manageComment";
    }

    @RequestMapping("/toEditComment/{type}/{id}")
    public String toEditComment(@PathVariable Integer type, @PathVariable Long id, Model model,@RequestParam Long houseId){
        if (type==2){
            if (null!=id && 0!=id){
                model.addAttribute("houseComment",houseCommentService.selectHouseCommentById(id));
            }
        }
        model.addAttribute("houseId",houseId);
        return "/tools/editComment";
    }

    @ResponseBody
    @RequestMapping("/list")
    public CommonResult list(@RequestParam(required = false) Integer minHouseScore, @RequestParam(required = false) Integer maxHouseScore,
                             @RequestParam Long houseId, @RequestParam(defaultValue = "1") int pageIndex,
                             @RequestParam(defaultValue = "10") int pageSize){
        logger.info("准备查询评分:{}分到:{}分的楼盘评价",minHouseScore,maxHouseScore);
        try {
            List<HouseComment> houseComments = houseCommentService.selectHouseCommentsByPage(minHouseScore, maxHouseScore, houseId, null,pageIndex, pageSize);
            for (HouseComment houseComment : houseComments){
                WeixinUser weixinUser = weixinUserService.getUserByUserid(houseComment.getUserid());
                houseComment.setUserNick(weixinUser.getNickname());
                houseComment.setHeadImg(weixinUser.getAvatarUrl());
            }
            int total = houseCommentService.selectTotal(minHouseScore, maxHouseScore, houseId,null);
            return CommonResult.createSuccessResult(houseComments,total,"查询指定楼盘成功");
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.createFailureResult("查询指定楼盘失败");
        }
    }

    @ResponseBody
    @RequestMapping("edit")
    public JsonResult edit(HouseComment houseComment,@RequestParam Long houseId){
        houseComment.setUserid(1L);
        if (null == houseComment.getId()) {
            houseComment.setCreateDatetime(new Date());
            houseComment.setHouseId(houseId);
            houseCommentService.saveHouseComment(houseComment);
            return new JsonResult(null,"添加楼盘评价成功");
        } else {
            houseCommentService.modifyHouseComment(houseComment);
            return new JsonResult(null,"更新楼盘评价成功");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public JsonResult delete(@PathVariable Long id){
        logger.info("准备删除id:{}的活动",id);
        try {
            houseCommentService.delete(id);
            return new JsonResult(null,"删除成功",true);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("发生业务异常,异常信息:{}",e.getMessage());
            return new JsonResult(null,"删除失败",false);
        }
    }

    @ResponseBody
    @RequestMapping("/listComment")
    public CommonResult listComment(@RequestParam(defaultValue = "1") Long userid, @RequestParam(defaultValue = "1") int pageIndex,
                             @RequestParam(defaultValue = "10") int pageSize) {
        List<House> houses = new ArrayList<>();
        try {
            List<HouseComment> houseComments = houseCommentService.selectHouseCommentsByPage(null,null,null,userid,pageIndex,pageSize);
            int total = houseCommentService.selectTotal(null,null,null,userid);
            for (HouseComment houseComment : houseComments) {
                House house = houseService.getHouseById(houseComment.getHouseId());
                houses.add(house);
            }
            Set<House> houseSet = new HashSet<>(houses);
            houses = new ArrayList<>(houseSet);
            return CommonResult.createSuccessResult(houses, total, "查询评论楼盘成功");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.createFailureResult("查询评论楼盘失败");
        }
    }

    @RequestMapping("/toHuifu/{id}")
    public String toHuifu(@PathVariable Long id,Model model){
        HouseComment houseComment = houseCommentService.selectHouseCommentById(id);
        model.addAttribute("houseComment",houseComment);
        return "/house/huifu";
    }

    @ResponseBody
    @RequestMapping("/editHuifu")
    public JsonResult editHuifu(HouseComment houseComment){
        try {
            houseCommentService.modifyHuifu(houseComment);
            return new JsonResult(null,"修改回复成功",true);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(null,"修改回复失败",false);
        }
    }
}
